package com.example.skycode6.dao;

import com.example.skycode6.bean.AdminBean;
import com.example.skycode6.util.DBConnection;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class AdminDao {
DBConnection DBConn = new DBConnection();
@@ -23,4 +24,93 @@ public class AdminDao {
}
return OK;
}
public List<AdminBean> getList(int page) {
Connection conn = DBConn.getConn();
String sql = "select * from admin limit "+(page-1)*10+",10";
List<AdminBean> list = new ArrayList<>();
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
AdminBean AdminBean = new AdminBean();
AdminBean.setAdminID(rs.getInt("adminID"));
AdminBean.setAdminName(rs.getString("adminName"));
AdminBean.setAdminPwd(rs.getString("adminPwd"));
AdminBean.setAdminType(1);
AdminBean.setLastLoginTime(rs.getString("lastloginTime"));
list.add(AdminBean);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return list;
}

/**
* @param adminBean 传入要更新的新闻对象,必须包含新闻id值
* @return 收影响行数
*/
public int update(AdminBean adminBean) {
Connection conn = DBConn.getConn();
if (adminBean.getAdminID() > 0) {
try {
Statement stmt = conn.createStatement();
String sql = "update admin set adminPwd='" + adminBean.getAdminPwd() + "'"
+ ",AdminType+'" + adminBean.getAdminType()+ "'"
+ ",AdminLastLoginTime='" + (new Date()).toString() + "'"//更新时使用当前系统时间
+ ",AdminName='" + "假管理员" + "'"//todo使用添加的管理员名称
+"where AdminID='"+adminBean.getAdminID()+"'";
return stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
return 0;
}

public int add(AdminBean adminBean) {
Connection conn = DBConn.getConn();
try {//使用预处理执行插入
String sql = "insert into admin(AdminName,AdminPwd,AdminType,AdminLastLoginTime) values(?,?,?,?)";
PreparedStatement pmst = conn.prepareStatement(sql);
pmst.setString(1,adminBean.getAdminName());//将第一个参数设置为标题内容
pmst.setString(2,adminBean.getAdminPwd());
pmst.setString(3,(new Date()).toString());
pmst.setString(4,"临时管理员");
return pmst.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
/**
* 删除功能
*/
public int deleteNewsByAdminID(int adminID) {
Connection conn = DBConn.getConn();
String sql = "delete from admin where AdminID=?";
try {
PreparedStatement pmst = conn.prepareStatement(sql);
pmst.setInt(1,adminID);
return pmst.executeUpdate();
} catch (SQLException e) {

}
return 0;
}
public int countPage(){
Connection conn = DBConn.getConn();
String sql = "select count(*) from admin";
try {
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery(sql);//获取查询结果集
if (rs.next()) {
return (rs.getInt(1)/10) +1;
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return 0;
}
}